<?php
namespace app\index\controller;
use think\Controller;
class Wxlogin extends Controller
{
	public function index()
	{
		$site_url = config('site_url');
		//scope=snsapi_base 实例
		$appid = config('appid');
		$redirect_uri = urlencode ( $site_url.'/index.php?s=/index/wxlogin/get_user_info.html' );
		$url ="https://open.weixin.qq.com/connect/oauth2/authorize?appid=$appid&redirect_uri=$redirect_uri&response_type=code&scope=snsapi_base&state=1#wechat_redirect";
		// header("Location:".$url);
		$this->redirect($url);
	}

    public function get_user_info()
    {
    	$appid = config('appid');  
		$secret = config('appsecret');  
		$site_url = config('site_url');
		$code = input('param.code');
		//第二步:取得openid
		$oauth2Url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=$appid&secret=$secret&code=$code&grant_type=authorization_code";
		$oauth2 = $this->getJson($oauth2Url);
		  
		//第三步:根据全局access_token和openid查询用户信息  
		$access_token = $this->getAccessToken();  
		$openid = $oauth2['openid'];  
		$get_user_info_url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=$access_token&openid=$openid&lang=zh_CN";
		$userinfo = $this->getJson($get_user_info_url);
		$model = db('users');
		$where = ['openid'=>$openid];
		$user = $model->where($where)->find();
		$data = [
			'nickname' => $userinfo['nickname'],
			'province' => $userinfo['province'],
			'city' => $userinfo['city'],
			'country' => $userinfo['country'],
			'headimgurl' => $userinfo['headimgurl'],
		];
		if (empty($user)) {
			$data['openid'] = $openid;
			$data['add_time'] = time();
			$model->insert($data);
		}else{
			$model->where($where)->update($data);
		}

		$user = $model->where($where)->find();
		session('user',$user);
		$tpid = cookie('tpid');
		$this->redirect($site_url.'/index.php?s=/index/index/index/tpid/'.$tpid.'.html');
    }

    public function getAccessToken()
    {
		// 取全局access_token
		$access_token_arr = db('wx_access_token')->where(['id'=>1])->find();
		$time = time();
		if ($access_token_arr['expire_time'] > $time) {
			$access_token = $access_token_arr['access_token'];
		}else{
	    	$appid = config('appid');  
			$secret = config('appsecret');  
			$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appid&secret=$secret";
			$token = $this->getJson($url);
			$access_token = $token['access_token'];
			$save = [
				'access_token' => $access_token,
				'add_time' => $time,
				'expire_time' => $time+7200,
			];
			db('wx_access_token')->where(['id'=>1])->update($save);
		}
		return $access_token;
    }

    public function getJson($url){
	    $ch = curl_init();
	    curl_setopt($ch, CURLOPT_URL, $url);
	    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 
	    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); 
	    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	    $output = curl_exec($ch);
	    curl_close($ch);
	    return json_decode($output, true);
	}
}
